﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using QUANLIBANHANG.ENTITY;

namespace QUANLIBANHANG.DAL
{
    public class KhachHangDAL
    {
        
       
        public static void insertKH(KhachhangEntity kh)
        {
            try
            {
                ConnectionSQL.commandSQL("INSERT INTO KHACHHANG (HOTEN, DIACHI, SDT, NGAYSINH, NGAYDK, TONGMUA, LOAIKH) VALUES (N'" + kh.Hoten + "',N'" + kh.Diachi + "','" + kh.Sdt + "'," + kh.Ngaysinh + "," + kh.Ngaydk + ",'" + kh.Tongmua + "',N'" + kh.Loaikh + "')"); 
            }
            catch (SqlException e)
            {
                
                throw e;
            }
            
        }

        public static void updateKh(KhachhangEntity kh)
        {
            try
            {

                ConnectionSQL.commandSQL("UPDATE KHACHHANG SET HOTEN=N'" + kh.Hoten + "',DIACHI=N'" + kh.Diachi + "',SDT='" + kh.Sdt + "',NGAYSINH=" + kh.Ngaysinh + ",NGAYDK=" + kh.Ngaydk + ",TONGMUA='" + kh.Tongmua + "',LOAIKH=N'" + kh.Loaikh + "' WHERE MAKH='" + kh.Makh + "'");
                
            }
            catch (SqlException e)
            {
                
                throw e;
            }
        }

        public static void deleteKH(KhachhangEntity kh)
        {
            try
            {
                ConnectionSQL.commandSQL("DELETE FROM KHACHHANG WHERE MAKH='" + kh.Makh + "'");
                
            }
            catch (SqlException e)
            {
                
                throw e;
            }
        }

        public static DataTable getTableKH(string dk)
        {
            try
            {
                return ConnectionSQL.getTable("SELECT * FROM KHACHHANG " + dk);
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
           
        }

        //public static DataRow getRowKH(string dk)
        //{
        //    try
        //    {
        //        return ConnectionSQL.getTable("SELECT * FROM KHACHHANG WHERE MAKH='" + dk + "'").Rows[0];

        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

        

        public static DataTable getTopKH()
        {
            try
            {
                return ConnectionSQL.getTable("SELECT TOP 10 KHACHHANG.MAKH, KHACHHANG.HOTEN, KHACHHANG.LOAIKH, KHACHHANG.TONGMUA  FROM KHACHHANG ORDER BY TONGMUA DESC");
            }
            catch (Exception)
            {
                
                throw;
            }
        }
    }
}
